package org.thanos.demo.service;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service("demoService")
public class DemoServiceImpl implements DemoService {

    private volatile AtomicInteger counter = new AtomicInteger(0);

    @Override
    public void hello(ChannelHandlerContext ctx, Object msg) {
        String body = String.valueOf(msg);
        try {
            log.info("The time server receive order:{}, counter:{}", body, counter.incrementAndGet());
            String currentTime = System.currentTimeMillis() + "";
            ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
            ctx.write(resp);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
